package collection;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 双端队列
 * java.util.deque接口时双端队列接口，它继承自Queue
 * 双端队列是队列两端都可以做出入队操作的队列
 * 常用实现类：java.util.LinkedList
 */
public class DequeDemo {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<>();
        deque.offer("one");
        deque.offer("two");
        deque.offer("three");
        System.out.println(deque);//[one, two, three]
        //从队首方向入队
        deque.offerFirst("four");
        System.out.println(deque);//[four, one, two, three]
        //从队尾方向入队
        deque.offerLast("five");
        System.out.println(deque);//[four, one, two, three, five]

        //从队首方向取出
        String str = deque.pollFirst();
        System.out.println(str);//four
        System.out.println(deque);//[one, two, three, five]
        //从队尾方向取出
        str = deque.pollLast();
        System.out.println(str);//five
        System.out.println(deque);//[one, two, three]
    }
}
